<?php

/**
 * @file grid.php
 * @brief Clase para generar un gridce
 * @author DyrTechnologies
 * @version V 1.0
 * @date Wed Oct 03 2007
 *
 *  Part of the CEFIRO dynamic web framework project
 *
 *  Copyright (C) 2007  Dyr technologies http://dyrtech.com.ve
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @class Clase gridce
 */

/**
 * @brief Define la ruta a la hoja de estilos, scripts e imagenes para el gridce
 *
 */

define("GRID_CSS",FULL_URL."grid/css/dhtmlXGrid.css");
define("COMMON_JS",FULL_URL."grid/js/dhtmlXCommon.js");
define("GRID_JS",FULL_URL."grid/js/dhtmlXGrid.js");
define("GRID_CELL_JS",FULL_URL."grid/js/dhtmlXGridCell.js");
define("GRID_IMG",FULL_URL."grid/img/");

function get_grid_scripts(){
	$CJS = "<script type=\"text/javascript\" src=\"".COMMON_JS."\"></script>";
	$GJS = "<script type=\"text/javascript\" src=\"".GRID_JS."\"></script>";
	$GCJS = "<script type=\"text/javascript\" src=\"".GRID_CELL_JS."\"></script>";
	return $CJS.$GJS.$GCJS;
}

function get_common_src(){
	return COMMON_JS;
}

function get_grid_src(){
	return GRID_JS;
}

function get_grid_cell_src(){
	return GRID_CELL_JS;
}

global $grid_instance;
$grid_instance = 0;

class gridce{

	private $fields;
	private $delimiter;
	private $headers;
	private $widths;
	private $colaligns;
	private $coltypes;
	private $colsortings;
	private $multiselect;
	private $on_row_select_handler;
	private $buffering;
	private $extras;

	private $name;

	/**
	 * @brief Constructor por omision y parametrico de la clase aprovechamos el hecho de que cada atributo tiene un valor por omision predefinido para no rehacer el constructor
	 * @param integer $_fields
	 * @param char $_delimiter
	 * @param Array of strings $_headers
	 * @param Array of strings $_widths
	 * @param Array of strings $_colaligns
	 * @param Array of strings $_coltypes
	 * @param Array of strings $_colsortings
	 * @param boolean $_multiselect
	 * @param string $_on_row_select_handler
	 * @param integer $_buffering
	 * @param string $_name
	 * @param string $_extras
	 * @return gridce La instancia de la clase
	 */

	public function gridce($_name='', $_fields=0,$_delimiter='|',$_headers='',$_widths='',$_colaligns='',$_coltypes='',$_colsortings='',$_multiselect=false,$_on_row_select_handler='',$_buffering=40, $_extras=''){

		/**
		 * @brief Se asignan los valores a los atributos de la clase grid segun vengan dados al llamar al constructor de la misma, notar que existen valores por omision para cada atributo
		 */
		$this->fields=$_fields;
		$this->delimiter=$_delimiter;
		$this->headers=$_headers;
		$this->widths=$_widths;
		$this->colaligns=$_colaligns;
		$this->coltypes=$_coltypes;
		$this->colsortings=$_colsortings;
		$this->multiselect=$_multiselect;
		$this->on_row_select_handler=$_on_row_select_handler;
		$this->buffering=$_buffering;
		$this->extras=$_extras;

		/**
		 * @brief $grid_instance es una variable para incrementar el numero de grids existentes
		 */
		global $grid_instance;
		$grid_instance++;

		/**
		 * @brief El nombre del widget es el tipo con el numero de la instancia que representa o el nombre pasado por omision en caso contrario
		 */

		if($_name!="__grid")
		$this->name=$_name;
		else
		$this->name="grid".$grid_instance;
	}


	/**
	 * @brief Las funciones get_VAR devuelven las variables que tenga ajustadas la instancia: Numero de Campos (fields), delimitador (delimiter), Cabeceras (headers), Anchos (widths), Alineaciones (colaligns), Tipos (coltypes), Orden (colsortings), Seleccion Multiple (multiselect), Manejador de Filas (on_row_select_handler), Nombre (name)
	 *
	 * @return string Valor de VAR
	 */

	public function get_name(){
		return $this->name;
	}

	public function get_fields(){
		return $this->fields;
	}

	public function get_delimiter(){
		return $this->delimiter;
	}

	public function get_headers(){
		return $this->headers;
	}

	public function get_widths(){
		return $this->widths;
	}

	public function get_aligns(){
		return $this->colaligns;
	}

	public function get_types(){
		return $this->coltypes;
	}

	public function get_sortings(){
		return $this->colsortings;
	}

	public function get_multiselect(){
		return $this->multiselect;
	}

	public function get_row_select_handler(){
		return $this->on_row_select_handler;
	}

	public function get_buffering(){
		return $this->buffering;
	}

	public function get_extras(){
		return $this->extras;
	}

	/**
	 * @brief Las funciones set_VAR ajustan las variables de la instancia: Numero de Campos (fields), delimitador (delimiter), Cabeceras (headers), Anchos (widths), Alineaciones (colaligns), Tipos (coltypes), Orden (colsortings), Seleccion Multiple (multiselect), Manejador de Filas (on_row_select_handler), Nombre (name)
	 */

	public function set_name($_name){
		$this->name=$_name;
	}

	public function set_fields($_fields){
		$this->fields=$_fields;
	}

	public function set_delimiter($_delimiter){
		$this->delimiter=$_delimiter;
	}

	public function set_headers($_headers){
		$this->headers=$_headers;
	}

	public function set_widths($_widths){
		$this->widths=$_widths;
	}

	public function set_aligns($_colaligns){
		$this->colaligns=$_colaligns;
	}

	public function set_types($_coltypes){
		$this->coltypes=$_coltypes;
	}

	public function set_sortings($_colsortings){
		$this->colsortings=$_colsortings;
	}

	public function set_multiselect($_multiselect){
		$this->multiselect=$_multiselect;
	}

	public function set_row_select_handler($_on_row_select_handler){
		$this->on_row_select_handler=$_on_row_select_handler;
	}

	public function set_buffering($_buffering){
		$this->buffering=$_buffering;
	}

	public function set_extras($_extras){
		$this->extras=$_extras;
	}

	/**
	 * @brief Ajusta los valores de smarty de acuerdo a lo especificado por los atributos de la instancia grid
	 */
	private function smarty_grid(){

		global $smarty;

		/**
		 * @brief Asignacion atributo por atributo a lo deseado en las variables de smarty
		 */
		$smarty->assign("name",$this->get_name());
		$smarty->assign("fields",$this->get_fields());
		$smarty->assign("delimiter",$this->get_delimiter());
		$smarty->assign("headers",$this->get_headers());
		$smarty->assign("widths",$this->get_widths());
		$smarty->assign("aligns",$this->get_aligns());
		$smarty->assign("types",$this->get_types());
		$smarty->assign("sortings",$this->get_sortings());
		$smarty->assign("multiselect",$this->get_multiselect());
		$smarty->assign("row_select_handler",$this->get_row_select_handler());
		$smarty->assign("buffering",$this->get_buffering());
		$smarty->assign("extras",$this->get_extras());
		global $grid_instance;
		$smarty->assign("numinstance",$grid_instance);

		/**
		 * @brief Asigna la hoja de estilo, scripts e imagenes que contiene los rasgos del grid a una variable
		 */
		$smarty->assign("gridcss",GRID_CSS);
		$smarty->assign("commonjs",COMMON_JS);
		$smarty->assign("gridjs",GRID_JS);
		$smarty->assign("gridcelljs",GRID_CELL_JS);
		$smarty->assign("gridimg",GRID_IMG);
	}

	/**
	 * @brief Asigna el grid a la variable $smarty para su renderizacion
	 *
	 */
	public function display_grid($print_scripts = true){
		global $smarty;
		$smarty->template_dir = dirname(__FILE__).'/';
		$this->smarty_grid();
		$smarty->assign("print_srcs","true");
		$smarty->assign("script_tag","true");
		$smarty->assign("headers","false");
		if($print_scripts){
		$smarty->assign("print_scripts","true");
		$smarty->assign("headers","true");
		}
		else{ 
		$smarty->assign("print_scripts","false");
		$smarty->assign("headers","false");
		}
		$smarty->assign( $this->name , $smarty->fetch('grid.tpl') );
	}
	
	public function get_scripts(){
		global $smarty;
		$smarty->template_dir = dirname(__FILE__).'/';
		$this->smarty_grid();
		$smarty->assign("print_srcs","false");
		$smarty->assign("script_tag","false");
		$smarty->assign("headers","false");
		$smarty->assign("print_scripts","true");
		return $smarty->fetch('grid.tpl');
	}
}

?>